package fr.inserm.u915.tabixserver;

import java.io.IOException;
import java.io.OutputStream;

import javax.xml.stream.XMLStreamException;

import fr.inserm.u915.tabixserver.expr.Context;
import fr.inserm.u915.tabixserver.expr.ExprNode;

public class HtmlHandler
	extends AbstractXmlHandler
	{
	public HtmlHandler(OutputStream out, String encoding)
			throws IOException
		{
		super(out, encoding);
		}
	@Override
	public void writeStartDocument() throws IOException
		{
		//dont write XML header
		try
			{
			getWriter().writeStartElement("html");
			getWriter().writeStartElement("head");
			getWriter().writeEndElement();//head
			getWriter().writeStartElement("body");
			}
		catch (XMLStreamException err)
			{
			throw new IOException(err);
			}
		}
	
	@Override
	public void writeEndDocument() throws IOException
		{
		try
			{
			getWriter().writeEndElement();
			getWriter().writeEndElement();
			getWriter().flush();
			getWriter().close();
			}
		catch (XMLStreamException err)
			{
			throw new IOException(err);
			}
		}
	
	@Override
	public void writeStartQuery(Segment segment) throws IOException
		{
		super.writeStartQuery(segment);
		try
			{
			getWriter().writeStartElement("div");
			}
		catch (XMLStreamException err)
			{
			throw new IOException(err);
			}
		
		}

	@Override
	public void writeEndQuery() throws IOException
		{
		try
			{
			getWriter().writeEndElement();
			}
		catch (XMLStreamException err)
			{
			throw new IOException(err);
			}
		super.writeEndQuery();
		}
	
	@Override
	public void writeStartInstance(Instance instance) throws IOException
		{
		super.writeStartInstance(instance);
		try
			{
			getWriter().writeStartElement("table");//table
			
			getWriter().writeStartElement("caption");
			getWriter().writeCharacters(getInstance().getLabel());
			getWriter().writeEndElement();
			
			getWriter().writeStartElement("thead");
			getWriter().writeStartElement("tr");
			for(int i=0;i< getInstance().size();++i)
				{
				Column c = getInstance().get(i);
				if(c.isIgnore()) continue;
				getWriter().writeStartElement("th");
				getWriter().writeCharacters(c.getLabel());
				getWriter().writeEndElement();
				}
			getWriter().writeEndElement();
			getWriter().writeEndElement();

			getWriter().writeStartElement("tbody");//tbody
			}
		catch (XMLStreamException err)
			{
			throw new IOException(err);
			}
		}
	
	@Override
	public void writeEndInstance() throws IOException
		{
		try
			{
			getWriter().writeEndElement();//tbody
			getWriter().writeEndElement();//table
			}
		catch (XMLStreamException err)
			{
			throw new IOException(err);
			}
		super.writeEndInstance();
		}
	
	

	@Override
	public int writeRow(String[] tokens) throws IOException
		{
		Context ctx=null;
		try
			{
			getWriter().writeStartElement("tr");
			for(int i=0;i< getInstance().size();++i)
				{
				Column c = getInstance().get(i);
				if(c.isIgnore()) continue;
				String url=null;
				if(c.getUrlExpr()!=null)
					{
					if(ctx==null)
						{
						ctx=new Context(getInstance(), tokens);
						}
					ExprNode n=c.getUrlExpr().eval(ctx);
				
					if(n!=null) url=n.toString();
					}
				getWriter().writeStartElement("td");
				if(url!=null)
					{
					getWriter().writeStartElement("a");
					getWriter().writeAttribute("href", url);
					}
				getWriter().writeCharacters(String.valueOf(tokens[c.getColumnIndex()]));
				if(url!=null)
					{
					getWriter().writeEndElement();
					}
				getWriter().writeEndElement();
				}
			getWriter().writeEndElement();
			}
		catch (XMLStreamException err)
			{
			throw new IOException(err);
			}
		return _next();
		}
	
	}
